﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using QS.Input;
using Microsoft.Xna.Framework.Input;
using QS.Logic.Objects;
using Microsoft.Xna.Framework.Graphics;
using QS.Drawables;
using QS.Game.Logic.Objects.common;
using QS.Game.Logic.Objects;

namespace QS.Game.Logic.Physics
{
    class QSpaceShipControl3D : QAbstractForce
    {
        private float mouseX = 0;
        private float mouseY = 0;

        public QSpaceShipControl3D( QPhysicsObject physicsObject)
            : base(physicsObject)
        {
        }

        protected override void updateForce()
        {
            if (!QMouseOperations.RightDown)
            {
                List<Keys> keys = QKeyboardOperations.KeysDown;
                if (keys.Contains(Keys.W))
                {
                    PhysicsObject.Force += Vector3.Transform(Vector3.Forward / 10000, this.PhysicsObject.Orientation);
                    ((QSpaceShip)this.PhysicsObject).EgnineIsOn = true;
                    keys.Remove(Keys.W);
                }
                else
                    ((QSpaceShip)this.PhysicsObject).EgnineIsOn = false;

                if (keys.Contains(Keys.S))
                {
                    PhysicsObject.Force += Vector3.Transform(Vector3.Backward / 10000, this.PhysicsObject.Orientation);
                    keys.Remove(Keys.S);
                }

                if (keys.Contains(Keys.D))
                {
                    this.PhysicsObject.Position += Vector3.Transform(Vector3.Right, this.PhysicsObject.Orientation) * 0.05f;
                    keys.Remove(Keys.D);
                }

                if (keys.Contains(Keys.A))
                {
                    this.PhysicsObject.Position += Vector3.Transform(Vector3.Left, this.PhysicsObject.Orientation) * 0.05f;
                    keys.Remove(Keys.A);
                }

                if (keys.Contains(Keys.Q))
                {
                    this.PhysicsObject.Position += Vector3.Transform(Vector3.Up, this.PhysicsObject.Orientation) * 0.05f;
                    keys.Remove(Keys.Q);
                }

                if (keys.Contains(Keys.Y))
                {
                    this.PhysicsObject.Position += Vector3.Transform(Vector3.Down, this.PhysicsObject.Orientation) * 0.05f;
                    keys.Remove(Keys.Y);
                }

                this.mouseX -= QMouseOperations.PositionDelta.X * 0.001f;
                this.mouseY -= QMouseOperations.PositionDelta.Y * 0.001f;

                if (QMouseOperations.LeftClicked)
                    QBulletManager.getInstance().createBullet(this.PhysicsObject.Position, this.PhysicsObject.Orientation);
            }

            Vector3 Forward = Vector3.Transform(Vector3.Forward, this.PhysicsObject.Orientation);
            float dotProduct = Vector3.Dot(Forward, this.PhysicsObject.Speed);
            float length = (dotProduct < 0 ? -1 : 1) * this.PhysicsObject.Speed.Length();

            this.PhysicsObject.Orientation = Matrix.CreateRotationX(mouseY) * Matrix.CreateRotationY(mouseX);
            this.PhysicsObject.Speed = Vector3.Transform(Vector3.Forward * length, this.PhysicsObject.Orientation);
        }
    }
}
